import typescript from 'rollup-plugin-typescript2'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import path from 'path'
import { getPackagesSync } from '@lerna/project'
import vue  from 'rollup-plugin-vue'
import babel from 'rollup-plugin-babel'
import { terser } from 'rollup-plugin-terser'
import commonjs from '@rollup/plugin-commonjs'

// 获取package.json 找到名字 以 skzz 开头的
const inputs = getPackagesSync().map(pck => pck.name).filter(name => {
  return name.includes('skzz') && name !== 'skzz-tool' && name !== 'skzz-theme-chalk' && name !== 'skzz-utils'
})
const extensions = ['.mjs', '.ts', '.js', '.tsx']
const noElPrefixFile = /(skzz-tool|skzz-theme-chalk|skzz-utils)/
export default inputs.map(name => {
  const pckName = name.split('skzz-')[1] // button icon
  return {
    input: path.resolve(__dirname, `../packages/${pckName}/index.ts`),
    output: {
      format: 'es',
      file: `lib/${pckName}/index.js`,
      paths(id) {
        // 替换包中的引用 import skzz-conf-coulum -> import ../conf-coulum
        if (/^skzz/.test(id)) {
          return id.replace('skzz-', '../')
        }
      },
    },
    plugins: [
      terser(),
      nodeResolve(),
      vue({
        target: 'browser',
      }),
      commonjs(),
      typescript({
        tsconfigOverride: {
          compilerOptions: { // 打包单个组件的时候不生成ts声明文件
            declaration: false,
          },
          exclude: [
            'node_modules',
            'website',
          ],
        },
      }),
      babel({
        exclude: 'node_modules/**',
        runtimeHelpers: true,
        extensions,
      }),
    ],
    external(id) { // 对vue本身 和 自己写的包 都排除掉不打包
      return /^vue/.test(id) || /^skzz-/.test(id) || /^element-plus/.test(id)
    },
  }
})
